package com.baidu.shoppingmall.auth.visitor;

import com.baidu.shoppingmall.auth.composite.AuthorityNode01;
import com.baidu.shoppingmall.auth.dao.AuthorityDAO;
import com.baidu.shoppingmall.auth.domain.AuthorityPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class DeleteAuthorityVisitor01 implements AuthorityVisitor01{

    @Autowired
    private AuthorityDAO authorityDAO;

    @Override
    public void visit(AuthorityNode01 authorityNode01) {
        QueryWrapper<AuthorityPO> queryWrapper = new QueryWrapper<>();
        Map<String, Object> map = new HashMap<>();
        map.put("parent_id", authorityNode01.getId());
        queryWrapper.allEq(map);
        List<AuthorityPO> authorityPOList = authorityDAO.selectList(queryWrapper);
        ArrayList<AuthorityNode01> authorityNode01List = new ArrayList<>();
        authorityPOList.forEach(po->authorityNode01List.add(po.clone(AuthorityNode01.class)));
        if (authorityNode01List!=null&&authorityNode01List.size()>0){
            authorityNode01List.forEach(node->node.accept(this));
        }
        authorityDAO.deleteById(authorityNode01.getId());
        System.out.println(authorityNode01.getAuthorityComment()+"被删除");
    }
}
